﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;


namespace OctopusWebInterface
{
    public partial class GetThumbnail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string guidStr = this.Request.QueryString.Get("guid");
            string widthStr = this.Request.QueryString.Get("width");

            if (guidStr == null || widthStr == null)
            {
                this.Response.ContentType = "text/plain";
                this.Response.Write("Sorry, invalid request");
                return;
            }

            Guid guid = new System.Guid(guidStr);
            uint width = UInt32.Parse(widthStr);
            uint height = width * 3u / 4u;

            using (Bitmap bmp = Octopus.OctopusLib.Functions.getThumbnail_or_blank(guid,width,height))
            {
                var ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Seek( 0L, System.IO.SeekOrigin.Begin );
                byte[] data = new byte[ms.Length];
                ms.Read(data, 0, (int)ms.Length);

                this.Response.ContentType = "image/jpeg";
                this.Response.Cache.SetNoServerCaching();
                this.Response.Cache.SetNoStore();
                this.Response.BinaryWrite(data);

                this.Response.Flush();
                this.Response.Close();
            } 
        }
    }
}